package com.demo.livemanagement.livedelayqueue;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class LiveDelayInfo implements Delayed {

	private long delay; // 延迟时间
	private Date expire; // 到期时间
	private String msg; // 数据
	private Date now; // 创建时间

	private long liveId;// 直播主键

	public LiveDelayInfo() {
		this.now = new Date();
	}
	public LiveDelayInfo(Date expire,String msg) {
		this.expire = expire;
		this.msg = msg;
	}

	/**
	 * 需要实现的接口，获得延迟时间 用过期时间-当前时间
	 * 
	 * @param unit
	 * @return
	 */
	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(this.expire.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	}

	/**
	 * 用于延迟队列内部比较排序 当前时间的延迟时间 - 比较对象的延迟时间
	 * 
	 * @param o
	 * @return
	 */
	@Override
	public int compareTo(Delayed o) {
		return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
	}

	public Date getExpire() {
		return expire;
	}

	public void setExpire(Date expire) {
		this.expire = expire;
	}

	public long getLiveId() {
		return liveId;
	}

	public void setLiveId(long liveId) {
		this.liveId = liveId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String toString() {
		return "LiveDelayInfo [delay=" + delay + ", expire=" + expire + ", msg=" + msg + ", now=" + now + ", liveId="
				+ liveId + "]";
	}

}
